package edu.sjsu.itack.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import edu.sjsu.itack.dao.BoardDao;
import edu.sjsu.itack.dao.TackDao;
import edu.sjsu.itack.entity.Board;
import edu.sjsu.itack.entity.Tack;
import edu.sjsu.itack.entity.User;
import edu.sjsu.itack.util.ITackException;

/**
 * Servlet implementation class AddTackServlet
 */
@WebServlet("/AddTackServlet")
public class AddTackServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddTackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("In AddServlet: doGet method");
		BoardDao board=new BoardDao();
		User user=(User)request.getSession().getAttribute("user");
		System.out.println(user.getUserId());
		try {
			System.out.println("In AddServlet: doget: try");
			Board[] boardArr=board.getAllBoardsForUser((int)user.getUserId());
		
	
			request.getSession().setAttribute("boardArr", boardArr);
			request.getRequestDispatcher("AddTack.jsp").forward(request, response);
		} catch (ITackException e) {
			System.out.println("In AddServlet: doget: exception");
			// TODO Auto-generated catch block
			//status.append(e.getLocalizedMessage());
			request.setAttribute("error", e.getLocalizedMessage());
			request.getRequestDispatcher("AddTack.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
System.out.println("In Addervlet: doPost method");
		
		String tackName= (String)request.getParameter("tackName");
		String src= (String)request.getParameter("tackPath");
		String tackDesc= (String)request.getParameter("tackDesc");
		int boardId= Integer.parseInt(request.getParameter("boardId"));
		String imagePath= (String)request.getParameter("imagePath");
		/*System.out.println("tackName="+tackName);
		System.out.println("tackPath="+tackPath);
		System.out.println("tackDesc="+tackDesc);
		System.out.println("boardId:" +boardId);*/
		Tack tack =new Tack();
		TackDao tackDao=new TackDao();
		tack.setImageName(tackName);
		tack.setImagePath(imagePath);
		tack.setBoardId(boardId);
		tack.setDescription(tackDesc);
		tack.setUrl(src);
		
		//Uploading Images to the Project folder
		/*File source = new File(File.separator+"Users"+File.separator+"krutirag"+File.separator+"git"+File.separator+"itak"+File.separator+"WebContent"+File.separator+"Images");
		File desc = new File(File.separator+"Users"+File.separator+"krutirag"+File.separator+"Pictures"+File.separator+"Kruti");
		String dest = "/Users/krutirag/git/itak/WebContent/images/testSuccess3.jpg";
		String src1 = "/Users/krutirag/Pictures/home3.jpg";
		try {
		    FileUtils.copyDirectory(source, desc);
		} catch (IOException e) {
		    e.printStackTrace();
		}
		ImageUpload imgup= new ImageUpload(src1,dest);*/
		try {
			tackDao.createTack(tack);
			JOptionPane.showMessageDialog(null, "Tack added!");
		} catch (ITackException e) {
			// TODO Auto-generated catch block
			request.setAttribute("error", e.getLocalizedMessage());
			request.getRequestDispatcher("AddTack.jsp").forward(request, response);
		}
	}

}
